package info.ask4research.util;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sun.org.apache.xpath.internal.XPathAPI;

public class WriteXMLFile extends DefaultHandler{

	public static List<String> langStrings = new ArrayList<String>();
	public static HashMap<String,String> learnerRoles = new HashMap<String,String>();
	public static HashMap<String,String> staffRoles = new HashMap<String,String>();
	public static HashMap<String,String> titleActivities = new HashMap<String,String>();
	public static HashMap<String,HashMap<String,Object>> activities = new HashMap<String,HashMap<String,Object>>();
	public static HashMap<String,HashMap<String,Object>> environments = new HashMap<String,HashMap<String,Object>>();
	public static HashMap<String,HashMap<String,Object>> resources = new HashMap<String,HashMap<String,Object>>();
	public static HashMap<String,HashMap<String,Object>> acts = new HashMap<String,HashMap<String,Object>>();
	public static HashMap<String,HashMap<String,Object>> actsOrdered = new HashMap<String,HashMap<String,Object>>();
	static XPath  xPath;
	static Document xmlDocument;
	
	
	static String titleXPath = "manifest//organizations//learning-design/title";
	
	static String learnersXPath = "manifest//organizations//learning-design/components/roles/learner";
	static String learnersTitleXPath = "manifest//organizations//learning-design/components/roles/learner/title";
	static String staffXPath = "manifest//organizations//learning-design/components/roles/staff";
	static String staffsTitleXPath = "manifest//organizations//learning-design/components/roles/staff/title";

	static String learningActivityXPath = "manifest//organizations//learning-design/components/activities/learning-activity";

	static String supportActivityXPath = "manifest//organizations//learning-design/components/activities/support-activity";

	static String activityStructureXPath = "manifest//organizations//learning-design/components/activities/activity-structure";

	static String environmentsXPath = "manifest//organizations//learning-design/components/environments/environment";

	static String playXPath = "manifest//organizations//learning-design/method/play/act/role-part";

	static String resourcesXPath = "manifest//resources/resource";

	static String learningActivityEnvironmentXPath = "manifest//organizations//learning-design/components/activities/learning-activity/environment-ref";

	public static void main(String argv[]) throws ParserConfigurationException, SAXException, IOException, TransformerException {


		File file = new File("D:\\imsmanifest.xml");
		Document doc = null;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();

		doc = db.parse(file);
		NodeList generalList = doc.getElementsByTagName("langstring");

		for( int i = 0 ; i<generalList.getLength(); i++){
			Node node = generalList.item(0).getFirstChild();
			System.out.println(node.getNodeValue());
		}

		try {
			xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
			xPath =  XPathFactory.newInstance().	newXPath();

			NodeList titleList = XPathAPI.selectNodeList(doc, titleXPath);
			Node nodeTitle = titleList.item(0);
			String title  = nodeTitle.getTextContent();
			System.out.println(title);
			NodeList learnersList = XPathAPI.selectNodeList(doc, learnersXPath);
			for(int i=0;i<learnersList.getLength(); i++){
				Node node = learnersList.item(i);
				NamedNodeMap namedNodeMap = node.getAttributes();
				String startId = namedNodeMap.getNamedItem("identifier").getTextContent();
				String titleValue = XPathAPI.selectNodeList(doc, learnersTitleXPath).item(0).getFirstChild().getNodeValue();
				learnerRoles.put(startId, titleValue);
			}

			NodeList staffList = XPathAPI.selectNodeList(doc, staffXPath);
			for(int i=0;i<staffList.getLength(); i++){
				Node node = staffList.item(i);
				NamedNodeMap namedNodeMap = node.getAttributes();
				String startId = namedNodeMap.getNamedItem("identifier").getTextContent();
				String titleValue = XPathAPI.selectNodeList(doc, staffsTitleXPath).item(0).getFirstChild().getNodeValue();
				staffRoles.put(startId, titleValue);
			}

			NodeList learningActivitiesList = XPathAPI.selectNodeList(doc, learningActivityXPath);
			for(int i=0;i<learningActivitiesList.getLength(); i++){
				Node node = learningActivitiesList.item(i);
				NamedNodeMap namedNodeMap = node.getAttributes();
				String startId = namedNodeMap.getNamedItem("identifier").getTextContent();
				NodeList nodes = node.getChildNodes();
				HashMap<String,Object> attributes = new HashMap<String,Object>();
				List<String> environments = new ArrayList<String>();
				String descTitle = "";
				String descItemRef = "";
				String descItemTitle = "";
				String completeActivity = "";
				for(int j = 0; j<nodes.getLength(); j++){
					Node innerNode = nodes.item(j);
					if("imsld:environment-ref".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						String ref = envAttrs.getNamedItem("ref").getTextContent();
						environments.add(ref);
					}else if("imsld:activity-description".equals(innerNode.getNodeName())){
						NodeList descs = innerNode.getChildNodes();
						for(int k = 0; k<descs.getLength(); k++){
							Node desc = descs.item(k);
							if("imsld:title".equals(desc.getNodeName())){
								descTitle = desc.getTextContent();
							}else if("imsld:item".equals(desc.getNodeName())){
								NamedNodeMap envAttrs = desc.getAttributes();
								String ref = envAttrs.getNamedItem("identifierref").getTextContent();
								descItemRef = ref;
								Node descItemTitleNode = desc.getChildNodes().item(0).getNextSibling();
								descItemTitle = descItemTitleNode.getFirstChild().getNodeValue();
							}
						}
					}else if("imsld:complete-activity".equals(innerNode.getNodeName())){
						NodeList descs = innerNode.getChildNodes();
						Node descItemTitleNode = descs.item(0);
						if(descs.getLength()==0){
							completeActivity = "none";
						}else  if(descs.getLength() > 0){
							if(descItemTitleNode.getNextSibling().getNodeName() == "imsld:user-choice"){
								completeActivity = "userChoice";
							}else{
								completeActivity = descItemTitleNode.getNextSibling().getFirstChild().getNodeValue();
							}
						}
					}
				}
				attributes.put("environments", environments);
				attributes.put("descTitle", descTitle);
				attributes.put("descItemRef", descItemRef);
				attributes.put("descItemTitle", descItemTitle);
				attributes.put("completeActivity", completeActivity);
				activities.put(startId, attributes);
			}


			NodeList supportActivitiesList = XPathAPI.selectNodeList(doc, supportActivityXPath);
			for(int i=0;i<supportActivitiesList.getLength(); i++){
				Node node = supportActivitiesList.item(i);
				NamedNodeMap namedNodeMap = node.getAttributes();
				String startId = namedNodeMap.getNamedItem("identifier").toString();
				NodeList nodes = node.getChildNodes();
				HashMap<String,Object> attributes = new HashMap<String,Object>();
				List<String> environments = new ArrayList<String>();
				List<String> roles = new ArrayList<String>();
				String descTitle = "";
				String descItemRef = "";
				String descItemTitle = "";
				String completeActivity = "";
				for(int j = 0; j<nodes.getLength(); j++){
					Node innerNode = nodes.item(j);
					if("imsld:environment-ref".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						String ref = envAttrs.getNamedItem("ref").toString();
						environments.add(ref);
					}else if("imsld:role-ref".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						String ref = envAttrs.getNamedItem("ref").toString();
						roles.add(ref);
					}else if("imsld:activity-description".equals(innerNode.getNodeName())){
						NodeList descs = innerNode.getChildNodes();
						for(int k = 0; k<descs.getLength(); k++){
							Node desc = descs.item(k);
							if("imsld:title".equals(desc.getNodeName())){
								descTitle = desc.getTextContent();
							}else if("imsld:item".equals(desc.getNodeName())){
								NamedNodeMap envAttrs = desc.getAttributes();
								String ref = envAttrs.getNamedItem("identifierref").toString();
								descItemRef = ref;
								Node descItemTitleNode = desc.getChildNodes().item(0).getNextSibling();
								descItemTitle = descItemTitleNode.getFirstChild().getNodeValue();
							}
						}
					}else if("imsld:complete-activity".equals(innerNode.getNodeName())){
						NodeList descs = innerNode.getChildNodes();
						Node descItemTitleNode = descs.item(0);
						if(descs.getLength()==0){
							completeActivity = "none";
						}else  if(descs.getLength() > 0){
							if(descItemTitleNode.getNextSibling().getNodeName() == "imsld:user-choice"){
								completeActivity = "userChoice";
							}else{
								completeActivity = descItemTitleNode.getNextSibling().getFirstChild().getNodeValue();
							}
						}
					}
				}
				attributes.put("environments", environments);
				attributes.put("roles", roles);
				attributes.put("descTitle", descTitle);
				attributes.put("descItemRef", descItemRef);
				attributes.put("descItemTitle", descItemTitle);
				attributes.put("completeActivity", completeActivity);
				activities.put(startId, attributes);
			}


			NodeList activityStructureList = XPathAPI.selectNodeList(doc, activityStructureXPath);
			for(int i=0;i<activityStructureList.getLength(); i++){
				Node node = activityStructureList.item(i);
				NamedNodeMap namedNodeMap = node.getAttributes();
				String startId = namedNodeMap.getNamedItem("identifier").toString();
				String structureType = namedNodeMap.getNamedItem("structure-type").toString();
				NodeList nodes = node.getChildNodes();
				HashMap<String,Object> attributes = new HashMap<String,Object>();
				List<String> la = new ArrayList<String>();
				List<String> sa = new ArrayList<String>();
				for(int j = 0; j<nodes.getLength(); j++){
					Node innerNode = nodes.item(j);
					if("imsld:learning-activity-ref".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						String ref = envAttrs.getNamedItem("ref").toString();
						la.add(ref);
					}else if("imsld:support-activity-ref".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						String ref = envAttrs.getNamedItem("ref").toString();
						sa.add(ref);
					}
				}
				attributes.put("structureType", structureType);
				attributes.put("la", la);
				attributes.put("sa", sa);
				activities.put(startId, attributes);
			}


			NodeList environmentList = XPathAPI.selectNodeList(doc, environmentsXPath);
			for(int i=0;i<environmentList.getLength(); i++){
				Node node = environmentList.item(i);
				NamedNodeMap namedNodeMap = node.getAttributes();
				String startId = namedNodeMap.getNamedItem("identifier").toString();
				NodeList nodes = node.getChildNodes();
				HashMap<String,Object> attributes = new HashMap<String,Object>();
				List<String> environmentsList = new ArrayList<String>();
				List<String> roles = new ArrayList<String>();
				List<String> items = new ArrayList<String>();
				String titl = "";
				String descTitle = "";
				String descItemRef = "";
				String descItemTitle = "";
				String conferenceManager = "";
				String moderator = "";;
				String serviceTitle = "";
				String type = "";
				for(int j = 0; j<nodes.getLength(); j++){
					Node innerNode = nodes.item(j);
					if("imsld:title".equals(innerNode.getNodeName())){
						title = innerNode.getChildNodes().item(0).getNodeValue();
					}else if("imsld:learning-object".equals(innerNode.getNodeName())){
						type = "LO";
						NodeList descs = innerNode.getChildNodes();
						for(int k = 0; k<descs.getLength(); k++){
							Node desc = descs.item(k);
							if("imsld:item".equals(desc.getNodeName())){
								NamedNodeMap envAttrs1 = desc.getAttributes();
								String ref1 = envAttrs1.getNamedItem("identifierref").toString();
								descItemRef = ref1;
								Node descItemTitleNode = desc.getChildNodes().item(0).getNextSibling();
								descItemTitle = descItemTitleNode.getFirstChild().getNodeValue();
							}
						}
					}else if("imsld:service".equals(innerNode.getNodeName())){
						NodeList descs = innerNode.getChildNodes();
						for(int k = 0; k<descs.getLength(); k++){
							Node desc = descs.item(k);
							if("imsld:conference".equals(desc.getNodeName())){
								type = desc.getAttributes().getNamedItem("conference-type").getTextContent();
								NodeList otherNodes = desc.getChildNodes();
								for(int s = 0; s< otherNodes.getLength(); s++){
									Node desc1 = otherNodes.item(s);
									if("imsld:conference-manager".equals(desc1.getNodeName())){
										NamedNodeMap envAttrs1 = desc1.getAttributes();
										String ref1 = envAttrs1.getNamedItem("role-ref").getTextContent();
										conferenceManager = ref1;
									}else if("imsld:moderator".equals(desc1.getNodeName())){
										NamedNodeMap envAttrs1 = desc1.getAttributes();
										String ref1 = envAttrs1.getNamedItem("role-ref").getTextContent();
										moderator = ref1;
									}else if("imsld:item".equals(desc1.getNodeName())){
										NamedNodeMap envAttrs1 = desc1.getAttributes();
										String ref1 = envAttrs1.getNamedItem("identifierref").getTextContent();
										descItemRef = ref1;
										Node descItemTitleNode = desc1.getChildNodes().item(0).getNextSibling();
										descItemTitle = descItemTitleNode.getFirstChild().getNodeValue();
									}
								}
							} else if("imsld:send-mail".equals(desc.getNodeName())){
								type = "send-mail";
								NodeList otherNodes = desc.getChildNodes();
								serviceTitle = otherNodes.item(0).getNodeValue();
							}
						}
					}

				}
				attributes.put("title", titl);
				attributes.put("descTitle", descTitle);
				attributes.put("descItemRef", descItemRef);
				attributes.put("serviceTitle", serviceTitle);
				attributes.put("descItemTitle", descItemTitle);
				attributes.put("moderator", moderator);
				attributes.put("type", type);
				attributes.put("conferenceManager", conferenceManager);
				environments.put(startId, attributes);
			}

			NodeList actsList = XPathAPI.selectNodeList(doc, playXPath);
			for(int i=0;i<actsList.getLength(); i++){
				Node node = actsList.item(i);
				NodeList nodes = node.getChildNodes();
				HashMap<String,Object> attributes = new HashMap<String,Object>();
				for(int j = 0; j<nodes.getLength(); j++){
					Node innerNode = nodes.item(j);
					if("imsld:support-activity-ref".equals(innerNode.getNodeName())){
						NamedNodeMap saAttrs = innerNode.getAttributes();
						String ref = saAttrs.getNamedItem("ref").toString();
						attributes.put("support", ref);
					}else if("imsld:learning-activity-ref".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						String ref = envAttrs.getNamedItem("ref").toString();
						attributes.put("learning", ref);
					}else if("imsld:role-ref".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						String ref = envAttrs.getNamedItem("ref").toString();
						attributes.put("role", ref);
					}
				}
				actsOrdered.put(String.valueOf(i), attributes);
			}


			NodeList resourcesList = XPathAPI.selectNodeList(doc, resourcesXPath);
			for(int i=0;i<resourcesList.getLength(); i++){
				Node node = resourcesList.item(i);
				NamedNodeMap namedNodeMap = node.getAttributes();
				String type = namedNodeMap.getNamedItem("type").toString();
				String identifier = namedNodeMap.getNamedItem("identifier").toString();
				String ref = "";
				NodeList nodes = node.getChildNodes();
				HashMap<String,Object> attributes = new HashMap<String,Object>();
				for(int j = 0; j<nodes.getLength(); j++){
					Node innerNode = nodes.item(j);
					if("file".equals(innerNode.getNodeName())){
						NamedNodeMap envAttrs = innerNode.getAttributes();
						ref = envAttrs.getNamedItem("href").toString();
					}
				}
				attributes.put("type", type);
				attributes.put("identifier", identifier);
				attributes.put("ref", ref);
				resources.put(identifier, attributes);
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (SAXException ex) {
			ex.printStackTrace();
		} catch (ParserConfigurationException ex) {
			ex.printStackTrace();
		}

	}

}
